/* eslint-disable no-eval */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ResultData } from '@/common/dto/result';
import { Injectable } from '@nestjs/common';
// import { InjectRepository } from '@nestjs/typeorm';
import { getConnection, getManager } from 'typeorm'; // , Repository
import {
  Lxd_Entity, // 求职，招聘
  // Lxd_position,   // 租房 买房
  // Lxd_SecondHand,  // 二手产品
  // Lxd_carEental,  // 拼车租车
  // Lxd_Enter  // 店铺入驻申请
} from './lxd.entity';
import {
  // Xzk_username,
  Xzk_usertype,
  // Xzk_ServiceLife,
} from '../xzk/xzk.entity';
// import { stringify } from 'querystring';

@Injectable()
export class LxdService {
  // 构造
  // 获取所有数据
  async upload() {
    // 方法操作数据库
    // entityManager ：实体管理器
    const entityManager = getManager();
    // entityManager ：查找
    const oss = await entityManager.find(Lxd_Entity);
    return ResultData.ok(oss);
    // return [];
  }

  // 模糊搜索
  async Seleces(data: any): Promise<ResultData> {
    // console.log('service', data.username);
    const userRepository = getManager();
    // 模糊搜索
    const res = await userRepository.find(Lxd_Entity, {
      where: `name like '%${data.username}%'`,
    });
    // console.log('res', res);

    return ResultData.ok(res);
  }

  // 删除
  async deletesdom(data: any): Promise<ResultData> {
    // console.log('service', data.userid);
    const userRepository = getManager();
    await userRepository
      .createQueryBuilder()
      .delete()
      .from(Lxd_Entity)
      .where('id = :id', { id: data.userid })
      .execute();
    // // 模糊搜索
    // const res = await userRepository.find(Lxd_Entity, { where: `name like '%${data.username}%'` });
    const entityManager = getManager();
    const res = await entityManager.find(Lxd_Entity);
    // console.log('res', res);

    return ResultData.ok(res);
    // return ResultData.ok('删除成功！');
  }
  // 批量删除
  async piliangdom(data: any): Promise<ResultData> {
    console.log('service', data.userid);
    const arr = Array.from(data.userid);
    console.log('arr', arr);

    const userRepository = getManager();
    arr.forEach(async (itme: any) => {
      await userRepository
        .createQueryBuilder()
        .delete()
        .from(Lxd_Entity)
        .where('id = :id', { id: itme })
        .execute();
    });

    const entityManager = getManager();
    const res = await entityManager.find(Lxd_Entity);
    console.log('res', res);

    return ResultData.ok(res);
    // return ResultData.ok('删除成功！');
  }

  // 修改状态 是否上下架
  async Soldoutdom(data: any): Promise<ResultData> {
    console.log('service', data.userType);
    const userRepository = getManager();
    if (data.userType.soldout === 1) {
      await userRepository
        .createQueryBuilder()
        .update(Lxd_Entity)
        .set({ soldout: 0 })
        .where('id = :id', { id: data.userType.id })
        .execute();
    } else {
      await userRepository
        .createQueryBuilder()
        .update(Lxd_Entity)
        .set({ soldout: 1 })
        .where('id = :id', { id: data.userType.id })
        .execute();
    }
    // 修改状态 是否上下架
    const entityManager = getManager();
    const res = await entityManager.find(Lxd_Entity);
    console.log('res', res);

    return ResultData.ok(res);
  }
  // --------------------------------------以下是 移动端的接口
  // 获取存入信息数据
  async uploadToken(data: any) {
    console.log('123123', data.objState);
    // 方法操作数据库
    // entityManager ：实体管理器
    const entityManager = getManager();
    await getConnection()
      .createQueryBuilder()
      .insert()
      .into(Xzk_usertype)
      .values([
        {
          name: data.objState.name,
          zhiwei: data.objState.zhiwei,
          recruit: data.objState.recruit, // 招聘职位
          nature: data.objState.nature, // 工作性质
          gender: data.objState.gender, // 性别
          experience: data.objState.experience, // 工作经验
          education: data.objState.education, // 学历
          pay: data.objState.pay, // 薪资
          address: data.objState.address, // 工作地址
          description: data.objState.description, // 职位描述
          welfare: data.objState.welfare, // 福利
          validity: data.objState.validity, // 有效期
          contacts: data.objState.contacts, // 联系人
          contactWay: data.objState.contactWay, // 联系方式
          EnterpriseImg: data.objState.EnterpriseImg, // 企业照片
        },
      ])
      .execute();
    const oss = await entityManager.find(Xzk_usertype);
    return ResultData.ok(oss);
    // return [];
  }
}
